package com.bangdao.app.jetpacktest;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;

import com.bangdao.app.jetpacktest.architecture.ArchitectureListActivity;
import com.bangdao.app.jetpacktest.decoration.RecycleViewDivider;
import com.bangdao.app.jetpacktest.jetpack.databinding.DataBindinglListActivity;
import com.bangdao.app.jetpacktest.jetpack.lifecycle.LifecycleListActivity;
import com.bangdao.app.jetpacktest.jetpack.livedata.LiveDataListActivity;
import com.bangdao.app.jetpacktest.jetpack.viewbinding.ViewBindingActivity;
import com.bangdao.app.jetpacktest.jetpack.viewmodel.ViewModelListActivity;
import com.bangdao.app.jetpacktest.architecture.mvp.view.UserListMVPActivity;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    public static final String[] ITEMS_NAME = {"LifeCycle", "ViewModel", "LiveData", "DataBinding", "ViewBinding", "Architecture"};
    private RecyclerView rv_content;
    private BaseQuickAdapter<String, BaseViewHolder> adapter;
    private List<String> contentDatas = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
        initData();
    }

    private void initView() {
        rv_content = findViewById(R.id.rv_content);

        adapter = new BaseQuickAdapter<String, BaseViewHolder>(R.layout.item_content) {
            @Override
            protected void convert(@NonNull BaseViewHolder holder, String bean) {
                holder.setText(R.id.tv_content, bean);
            }
        };
        adapter.setOnItemClickListener((adapter, view, position) -> {
            jumpMenu(position);
        });
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rv_content.setLayoutManager(layoutManager);
        if (rv_content.getItemDecorationCount() == 0) {
            rv_content.addItemDecoration(new RecycleViewDivider(this, LinearLayoutManager.VERTICAL,
                    R.drawable.list_item_driver));
        }
        rv_content.setAdapter(adapter);
    }

    private void initData() {
        contentDatas.clear();
        for (int i = 0; i < ITEMS_NAME.length; i++) {
            contentDatas.add(ITEMS_NAME[i]);
        }
        adapter.setNewInstance(contentDatas);
    }

    private void jumpMenu(int position) {
        String menuName = ITEMS_NAME[position];
        if("LifeCycle".equals(menuName)){
            Intent intent = new Intent(this, LifecycleListActivity.class);
            startActivity(intent);
        }
        if("ViewModel".equals(menuName)){
            Intent intent = new Intent(this, ViewModelListActivity.class);
            startActivity(intent);
        }
        if("LiveData".equals(menuName)){
            Intent intent = new Intent(this, LiveDataListActivity.class);
            startActivity(intent);
        }
        if("DataBinding".equals(menuName)){
            Intent intent = new Intent(this, DataBindinglListActivity.class);
            startActivity(intent);
        }
        if("ViewBinding".equals(menuName)){
            Intent intent = new Intent(this, ViewBindingActivity.class);
            startActivity(intent);
        }
        if("Architecture".equals(menuName)){
            Intent intent = new Intent(this, ArchitectureListActivity.class);
            startActivity(intent);
        }
    }
}